package itsm.isperp.module.service.configuration;

import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.entity.configuration.ConfCiRelationUsePropety;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.repository.configuration.ConfCiRelationUsePropetyMapper;
import itsm.isperp.module.service.db.DbEntityFieldService;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 配置关系服务
 * 
 * @author lizx
 * @date 2014-09-23 16:24
 */
@Service
public class ConfCiRelationUsePropetyService extends
		BaseService<ConfCiRelationUsePropety> {

	@Autowired
	protected ConfCiRelationUsePropetyMapper confCiRelationUsePropetyMapper;

	@Autowired
	protected DbEntityFieldService dbEntityFieldService;

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Override
	public ConfCiRelationUsePropetyMapper getRepository() {
		return this.confCiRelationUsePropetyMapper;
	}

	@Override
	public DataResponse<Map<String, Object>> findAllDetailByRequest(
			DataRequest request) {
		DataResponse<Map<String, Object>> re = this.getRepository()
				.findAllDetailByRequest(request);
		for (Map<String, Object> map : re) {
			Object o = map.get("from_property_id");
			if (o != null) {
				if (o.toString().contains("field-")) {
					DbEntityField field = this.dbEntityFieldService
							.findOneByTableNameAndColumn("conf_ci", o
									.toString().substring(6));
					if (field != null) {
						map.remove("from_property_name");
						map.put("from_property_name", field.getName());
					}
				}
			}
		}

		return re;
	}

	public void deleteByRelationTypeId(String relationTypeId) {
		jdbcTemplate
				.execute("DELETE FROM conf_ci_relation_use_propety WHERE relation_type_id='"
						+ relationTypeId + "'");
	}

	public void save(String relationTypeId, String[] fromPropertyIds,
			String[] toPropertyIds) {
		this.deleteByRelationTypeId(relationTypeId);
		for (int i = 0; i < fromPropertyIds.length; i++) {
			String fromPropertyId = fromPropertyIds[i];
			String toPropertyId = fromPropertyIds[i];
			ConfCiRelationUsePropety entity = new ConfCiRelationUsePropety();
			entity.setFromPropertyId(fromPropertyId);
			entity.setToPropertyId(toPropertyId);
			entity.setRelationTypeId(relationTypeId);
			this.saveOrUpdate(entity);
		}

	}

	/**
	 * 根据主管连的资产id及关联类型id获取所有关联属性
	 * 
	 * @param fromId
	 * @param relationTypeId
	 * @return
	 */
	public List<ConfCiRelationUsePropety> findByFromIdAndRelationTypeId(
			String fromId, String relationTypeId) {

		return this.confCiRelationUsePropetyMapper
				.findByFromIdAndRelationTypeId(fromId, relationTypeId);
	}

	public List<ConfCiRelationUsePropety> findByRelationTypeId(
			String relationTypeId) {
		return this.confCiRelationUsePropetyMapper
				.findByRelationTypeId(relationTypeId);
	}

	public List<ConfCiRelationUsePropety> findByFromId(String fromId) {
		return this.confCiRelationUsePropetyMapper.findByFromId(fromId);
	}

	public List<ConfCiRelationUsePropety> findByFromIdOrToId(String ciId) {
		return this.confCiRelationUsePropetyMapper.findByFromIdOrToId(ciId);
	}

}
